An Armstrong number (also known as a narcissistic number or plenary number) is a number that is equal to the sum of its own digits raised to the power of the number of digits.
def is_armstrong_number(number):
num_str = str(number)
num_digits = len(num_str)
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
return armstrong_sum == number
# Function to find Armstrong numbers in a range
def find_armstrong_numbers(start, end):
armstrong_numbers = []
for num in range(start, end + 1):
if is_armstrong_number(num):
armstrong_numbers.append(num)
return armstrong_numbers
# Taking input from the user for the range
start_range = int(input("Enter the start of the range: "))
end_range = int(input("Enter the end of the range: "))
# Finding Armstrong numbers within the specified range
armstrong_numbers_in_range = find_armstrong_numbers(start_range, end_range)
print("Armstrong numbers in the range", start_range, "to", end_range, "are:", armstrong_numbers_in_range)
Enter the start of the range: 100
Enter the end of the range: 1000
Armstrong numbers in the range 100 to 1000 are: [153, 370, 371, 407]
The function is_armstrong_number(number)
checks whether a given number is an Armstrong number or not.
The function find_armstrong_numbers(start, end)
finds Armstrong numbers within a specified range by iterating through each number in the range and checking if it's an Armstrong number.
The example usage section demonstrates how to take input from the user for the range, find Armstrong numbers within the specified range, and print the result.